package com.lbs.jiepiaisi.biz;

import android.content.Intent;

import com.lbs.jiepiaisi.bean.NewsEntity;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.parser.NewsParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.Response;

/**
 *
 * 时间：2018/1/11 12:04
 * 作用：
 */

public class NewsBiz {
    //根据经纬度查询地址
    public void getNews(String type,final String flag){
        if(!Constant.IS_USE_NEWS){
            NewsEntity newsEntity =new NewsEntity();
            newsEntity.setError_code(Constant.STATUS_NO_NET);
            Intent intent = new Intent(flag);
            intent.putExtra(Constant.KEY_DATA, newsEntity);
            App.instance.sendBroadcast(intent);
            return;
        }
        //获得sp存储的部门id
        FormBody formBody = new FormBody
                .Builder()
                .add("type",type)//设置参数名称和参数值/*String.valueOf(deptId)*/
                .add("key",Constant.NEWS_KEY)
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.NEW_LIST_URL, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {

                NewsEntity newsEntity= new NewsEntity();
                try{
                    //返回结果
                    String jsonString = response.body().string();
                    newsEntity= NewsParser.parser(jsonString);
                    newsEntity.setError_code(Constant.STATUS_OK);
                    if(new JSONObject(jsonString).getInt("error_code") != 0){
                        newsEntity.setError_code(Constant.STATUS_NETWORK_ERROR);
                    }
                }catch (Exception e){
                    newsEntity.setError_code(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                    Intent intent = new Intent(flag);
                    intent.putExtra(Constant.KEY_DATA, newsEntity);
                    App.instance.sendBroadcast(intent);
                }finally {
                    Intent intent = new Intent(flag);
                    intent.putExtra(Constant.KEY_DATA, newsEntity);
                    App.instance.sendBroadcast(intent);
                }
            }
            @Override
            public void failed() {
                NewsEntity newsEntity =new NewsEntity();
                newsEntity.setError_code(Constant.STATUS_NO_NET);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, newsEntity);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void noNetwork() {
                NewsEntity newsEntity=new NewsEntity();
                newsEntity.setError_code(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, newsEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }
}
